home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / zapem-0.000 / zapem-0 / zapem / Joystick.cc < prev    next >
C/C++ Source or Header  |  1995-06-06  |  2KB  |  110 lines

  1. /*    Copyright Alex Hornby 1994/1995. All rights reserved.
  2.      See file README for details
  3. */
  4. #ifdef JOYSTICK
  5.  
  6. #include "Joystick.h"
  7. #include <iostream.h>
  8. #include <stdlib.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <vga.h>
  12.  
  13. Joystick::Joystick(int j) : Control()
  14. {
  15.     raw=false;
  16.     okay=true;
  17.     if(j!=1 && j!=2) 
  18.     {
  19.         cerr << "Joystick out of range" << endl;
  20.         exit(1);
  21.     }
  22.     init(j);
  23.     if(okay==true)
  24.         calibrate();
  25. }
  26.  
  27. void 
  28. Joystick::waitfire(void)
  29. {
  30.     while( !(js_data.buttons & 1) )
  31.         read (joyfd, &js_data, JS_RETURN);
  32.     while( (js_data.buttons & 1) )
  33.         read (joyfd, &js_data, JS_RETURN);
  34. }
  35.  
  36. void
  37. Joystick::calibrate(void)
  38. {
  39.     cout << "Calbrating joystick..." << endl;
  40.     read (joyfd, &js_data, JS_RETURN);
  41.     cout << "Centre joystick and press fire\n";
  42.     waitfire();
  43.     centre.x=js_data.x; centre.y=js_data.y;
  44.     cout << "Move joystick to top left and press fire\n";
  45.     waitfire();
  46.     topleft.x=js_data.x; topleft.y=js_data.y;
  47.     cout << "Move joystick to bottom right and press fire\n";
  48.     waitfire();
  49.     botright.x=js_data.x;botright.y=js_data.y;
  50.  
  51.     right=centre.x+(botright.x-centre.x)/2;
  52.     left=centre.x-(centre.x-topleft.x)/2;
  53.     down=centre.y+(botright.y-centre.y)/2;
  54.     up=centre.y-(centre.y-topleft.y)/2;
  55. }
  56.  
  57. void
  58. Joystick::fetch(void)
  59. {
  60.     int status = read (joyfd, &js_data, JS_RETURN);
  61.         if (status != JS_RETURN)
  62.         {
  63.                 perror ("Joystick::read()");
  64.         }
  65.     for(int i=0;i<=FIRE4;i++)
  66.         last[i]=false;
  67.  
  68.     if(js_data.x>right)
  69.         last[RIGHT]=true;
  70.     if(js_data.x<left)
  71.         last[LEFT]=true;
  72.     if(js_data.y>down)
  73.         last[DOWN]=true;
  74.     if(js_data.y<up)
  75.         last[UP]=true;
  76.     if(js_data.buttons & 1) 
  77.         last[FIRE1]=true;
  78.     if(js_data.buttons & 2) 
  79.         last[FIRE2]=true;
  80.     int key=vga_getkey();
  81.     if(key==27)
  82.         last[QUIT]=true;
  83.     if(key=='p')
  84.         last[PAUSE]=true;
  85. }
  86.  
  87.  
  88. void
  89. Joystick::init(int stick)
  90. {
  91.     /* open joystick device file */
  92.     switch(stick)
  93.     {
  94.     case 1:
  95.             joyfd = open ("/dev/js0", O_RDONLY);
  96.         break;
  97.     case 2:
  98.         joyfd = open ("/dev/js1", O_RDONLY);
  99.         break;
  100.     }
  101.     
  102.         if (joyfd < 0) {
  103.                 //perror ("js init");
  104.                 //exit (1);
  105.         okay=false;
  106.         }
  107. }
  108.  
  109. #endif
  110.